<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
error_reporting(E_ERROR);
/*
 * 检查参数是否设置
 * @param array $params 需要检查的参数数组
 */
function checkParam($params){
    if(!$params){
        return false;
    }
    $bol = true;
    if(is_array($params)){
        foreach ($params as $v){
            if(!isset($v)){
                $bol = false;
                continue;
            }
        }
    }else{
        $bol = !isset($params)? false : true;
    }
    return $bol;
}

/*
 * 模型对象转数组，设置转换格式
 * @param array $obj 模型对象数据集
 */
function objToArray($obj) {
    if ($obj) {
        foreach ($obj as &$v) {
            $v = $v->toArray();
        }
    }
    return $obj?:[];
}
/*
 * 获取订单编号
 */
function orderNo($id,$time){
    return 'NO'.date('Ym',strtotime($time)).str_pad($id,8,0,STR_PAD_LEFT);
}

/**
 * 格式化身份证
 */
function formatIdno($idNo) {
    $len = strlen($idNo);
    if ($len != 15 && $len != 18) {
        return "";
    }
    $birth = strlen($idNo) == 15 ? ('19' . substr($idNo, 6, 6)) : substr($idNo, 6, 8);
    $data['birth'] = substr($birth, 0, 4) . "-" . substr($birth, 4, 2) . "-" . substr($birth, 6, 2);
    $today = strtotime('today');
    $date = strtotime($data['birth']);
    $diff = floor(($today - $date) / 86400 / 365);
    $age = strtotime(substr($idNo, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff - 1) : $diff;
    $data['age'] = $age;
    switch ($len) {
        case 15:
            if (substr($idNo, 14, 1) % 2 == 0) {
                $sex = "2";
            } else {
                $sex = "1";
            }
            break;
        case 18:
            if (substr($idNo, 14, 3) % 2 == 0) {
                $sex = "2";
            } else {
                $sex = "1";
            }
            break;
    }
    $data['sex'] = $sex;
    return $data;
}
/**
 * 验证身份证号码
 * @param $id 身份证号码
 * @return true | false
 */
function isIdcard($id) {
    $id = strtoupper($id);
    $regx = "/(^\d{15}$)|(^\d{17}([0-9]|X)$)/";
    $arr_split = array();

    if (!preg_match($regx, $id)) {
        return FALSE;
    }
    if (15 == strlen($id)) { //检查15位 
        $regx = "/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/";
        @preg_match($regx, $id, $arr_split);
        //检查生日日期是否正确 
        $dtm_birth = "19" . $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) {
            return FALSE;
        } else {
            return TRUE;
        }
    } else { //检查18位 
        $regx = "/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/";
        @preg_match($regx, $id, $arr_split);
        $dtm_birth = $arr_split[2] . '/' . $arr_split[3] . '/' . $arr_split[4];
        if (!strtotime($dtm_birth)) { //检查生日日期是否正确
            return FALSE;
        } else {
            //检验18位身份证的校验码是否正确。 
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
            $arr_int = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
            $arr_ch = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
            $sign = 0;
            for ($i = 0; $i < 17; $i++) {
                $b = (int) $id{$i};
                $w = $arr_int[$i];
                $sign += $b * $w;
            }
            $n = $sign % 11;
            $val_num = $arr_ch[$n];
            if ($val_num != substr($id, 17, 1)) {
                return FALSE;
            } else { //phpfensi.com 
                return TRUE;
            }
        }
    }
}

// 验证提交参数不能为空
function emptyParams($arr) {
    $flag = true;
    if (is_array($arr) && !empty($arr)) {
        foreach ($arr as $key => $val) {
            if (is_array($val)) {
                foreach ($val as $k => $v) {
                    if (!empty($v)) {
                        $flag = true;
                    } else {
                        return $flag = false;
                    }
                }
            } else {
                if (!empty($val)) {
                    $flag = true;
                } else {
                    return $flag = false;
                }
            }
        }
    } else {
        if (!empty($arr)) {
            $flag = true;
        } else {
            return $flag = false;
        }
    }

    return $flag;
}

// 判断真实姓名 支持少数民族(·)  2--20位
function isChinese($username = '') {
    return preg_match("/^([\x{4e00}-\x{9fa5}]){1,7}(?:·[\x{4e00}-\x{9fa5}]{1,7})*$/u",$username);
}
/**
 * 根据身份证号获取年龄
 * $idcard 身份证号
 * return $age 年龄
 */
function getAge($idcard)
{
    // 获得出生年月日的时间戳 
    $date = strtotime(substr($idcard,6,8));
    // 获得今日的时间戳 
    $today = strtotime('today');
    // 得到两个日期相差的大体年数 
    $diff = floor(($today-$date)/86400/365);
    // strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比 
    $age = strtotime(substr($idcard,6,8).' + '.$diff.'years')>$today?($diff-1):$diff; 

    return $age; 
}


/**
 * 判断客户端扫码来源
 */
function IsWeixinOrAlipay()
{
    $AGENT = $_SERVER['HTTP_USER_AGENT'];
    // 判断是不是微信
    if(strpos($AGENT, 'MicroMessenger') !== false){
        return "WeiXin";
    }else if(strpos($AGENT, 'AlipayClient') !== false){
        return "Alipay";
    }else{
        return "false";
    }
}

/**
 * 生成随机字符串
 * @param $length 字符串个数
 */
function getRandomChar($length = 8){
    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
        'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
        't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    $password = '';
    for($i = 0; $i < $length; $i++)
    {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[rand(0,count($chars)-1)];
    }
    return $password;
}

//curl函数
function curlOpen ($url, $cfg)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    if ($cfg['ssl']) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    //post提交方式
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $cfg['post']);
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
//        print_r($info);exit;
    curl_close($ch);

    return $result;
}

// 判断客户终端类型
function userAgent()
{
    //获取USER AGENT
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);

    //分析数据
    $is_pc = (strpos($agent, 'windows nt')) ? true : false;  
    $is_iphone = (strpos($agent, 'iphone')) ? true : false;  
    // $is_ipad = (strpos($agent, 'ipad')) ? true : false;  
    $is_android = (strpos($agent, 'android')) ? true : false;

    // 判断类型
    if($is_pc){  
       return "PC";
    }  
    if($is_iphone){
        return "iPhone";  
    }  
    // if($is_ipad){  
    //     return "iPad";  
    // }  
    if($is_android){  
        return "Android";
    } 
}
